package com.travelms.controller;

import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.Stack;

public class MianPage {

    public MianPage() throws IOException, SQLException, InterruptedException, InstantiationException, IllegalAccessException {
        //主界面
        //用stack来表示层级结构以便更好返回上一级
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        String s;//中间效验输入是否为数字
        int current = 0;//记录当前目录层级
        stack.push(current);//初始化目录层级为0

        while (true){
            DBFind dbFind = new DBFind();//进入主界面则会创建数据库连接
            if (current == 0){ //主界面
                System.out.println("欢迎进入旅游管理系统");
                System.out.println("1.普通用户登录");
                System.out.println("2.管理员登录");
                System.out.println("3.用户注册");
                System.out.println("4.忘记密码");
                System.out.println("5.退出");
                System.out.println("欢迎进入旅游管理系统");
                System.out.println("-----------------");
                while (true){
                    System.out.println("请输入您的选项:")  ;
                    s = sc.nextLine();
                    if (!DataEffect.isNumber(s)){//输入效验
                        System.out.println("请输入数字");
                    }else if(!(Integer.parseInt(s) > 0 && Integer.parseInt(s) <= 5)){
                        System.out.println("输入超出范围");
                        //必须大于0且小于5
                    }else {
                        current = Integer.parseInt(s);//记录输入的层级
                        stack.push(current);//压入栈
                        break;
                    }

                }
                if(current == 5){ //如果输入为4则退出系统
                    System.out.println("期待与您再次见面");
                    break;
                }


            }else if (current == 1){//普通用户登录

                //账号密码效验,
                while (true) {
                    System.out.println("输入exit退出输入");
                    System.out.printf("请输入账号：");
                    String ac = sc.nextLine();
                    if("exit".equals(ac)){
                        break;
                    }
                    System.out.printf("请输入密码：");
                    String password = sc.nextLine();


                    //这里为访问数据库返回的结果是否为真
                    if(dbFind.isRepeatUser(ac)){//
                        System.out.println("该用户已经登录，请换个账号");
                    }else if(!dbFind.isContainAccountAndPassword(ac,password)){
                        System.out.println("账号或者密码错误");
                    }else {

                        System.out.println("登录成功");
                        CommonUserPage.passwordStatus = 0;
                        dbFind.writeUser(ac);
                        dbFind.connection.close();
                        new CommonUserPage(ac);

                        break;
                        //是真则调用相应的界面
                    }
                }


                //当次级界面退出，肯定就是返回上一级，我们直接把current设置为上一级目录
                stack.pop();
                current = stack.peek();

            }else if (current == 2){//管理员登录
                //账号密码效验,
                while (true) {
                    System.out.println("输入exit退出输入");
                    System.out.printf("请输入账号：");
                    String ac = sc.nextLine();
                    if("exit".equals(ac)){
                        break;
                    }
                    System.out.printf("请输入密码：");
                    String password = sc.nextLine();
                    //这里为访问数据库返回的结果是否为真


                    if(dbFind.isContainAdmin(ac,password)){
                        System.out.println("登录成功");
                        dbFind.connection.close();
                        AdminUserPage.adminPasswordStatus = 0;
                        new AdminUserPage(ac);

                        break;
                        //是真则调用相应的界面
                    }else {
                        System.out.println("账号或者密码错误");
                    }
                }
                stack.pop();
                current = stack.peek();

            }else if (current == 3){//用户注册
                new Register();
                stack.pop();
                current = stack.peek();

            }else if (current == 4){//用户注册
                new ForgetPassword();
                stack.pop();
                current = stack.peek();

            }
        }
    }
}
